package cn.edu.dgut.css.springboottestthree.controller;

import cn.edu.dgut.css.springboottestthree.dao.RegionDao;
import cn.edu.dgut.css.springboottestthree.entity.Region;
import cn.edu.dgut.css.springboottestthree.result.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @date: 2020/11/16 16:57
 * @author: ZkhEight
 */
@Controller
public class RegionController {


    @Resource
    private RegionDao regionDao;

    @GetMapping("data")
    public String getData(Model model) throws IOException {
        model.addAttribute("region", regionDao.getRegions());
        return "index";
    }


    @GetMapping("search")
    public String Search(Model model , String search) throws IOException {
        List<Region> regions = regionDao.getRegions();
        List<Region> searchs = new ArrayList<>();
        if (search != null) {
            for (Region region : regions) {
                if (search.equals(region.getCountryName()) || search.equals(region.getProvinceName())) {
                    searchs.add(region);
                }
            }
        }
        model.addAttribute("region", searchs);
        return "index";
    }


}
